package javaseof10day.demo.usersadmin3;


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* TypeForm.java
*
* Created on 2013-2-26, 19:07:51
*/


import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author asus
*/
public class TypeForm extends javax.swing.JFrame {

   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private TypeDAO typeDao;
   private Map<Integer,TypeBean> map;
   /** Creates new form TypeForm */
   public TypeForm() {
       initComponents();
       typeDao = new TypeDAO();
       map = typeDao.readType();
       initData();
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
   private void initComponents() {

       jPanel1 = new javax.swing.JPanel();
       jScrollPane1 = new javax.swing.JScrollPane();
       typeTable = new javax.swing.JTable();
       jPanel2 = new javax.swing.JPanel();
       jLabel1 = new javax.swing.JLabel();
       jLabel2 = new javax.swing.JLabel();
       jLabel3 = new javax.swing.JLabel();
       tid = new javax.swing.JTextField();
       tname = new javax.swing.JTextField();
       jScrollPane2 = new javax.swing.JScrollPane();
       tmemo = new javax.swing.JTextArea();
       jButton1 = new javax.swing.JButton();
       jButton3 = new javax.swing.JButton();
       jButton4 = new javax.swing.JButton();
       jButton5 = new javax.swing.JButton();

       setTitle("类型管理");
       setBounds(new java.awt.Rectangle(100, 50, 0, 0));
      // setIconImage(new javax.swing.ImageIcon(getClass().getResource("/com/liu/resouce/logo.jpg")).getImage());

       typeTable.setFont(new java.awt.Font("宋体", 0, 18));
       typeTable.setModel(new javax.swing.table.DefaultTableModel(
           new Object [][] {
               {null, null, null},
               {null, null, null},
               {null, null, null},
               {null, null, null}
           },
           new String [] {
               "序号", "类别名称", "备注"
           }
       ) {
           /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		Class[] types = new Class [] {
               java.lang.Integer.class, java.lang.String.class, java.lang.String.class
           };
           boolean[] canEdit = new boolean [] {
               false, false, false
           };

           public Class getColumnClass(int columnIndex) {
               return types [columnIndex];
           }

           public boolean isCellEditable(int rowIndex, int columnIndex) {
               return canEdit [columnIndex];
           }
       });
       typeTable.setColumnSelectionAllowed(true);
       typeTable.addMouseListener(new java.awt.event.MouseAdapter() {
           public void mouseClicked(java.awt.event.MouseEvent evt) {
               typeTableMouseClicked(evt);
           }
       });
       typeTable.addContainerListener(new java.awt.event.ContainerAdapter() {
           public void componentAdded(java.awt.event.ContainerEvent evt) {
               typeTableComponentAdded(evt);
           }
       });
       jScrollPane1.setViewportView(typeTable);
       typeTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
       typeTable.getColumnModel().getColumn(0).setResizable(false);
       typeTable.getColumnModel().getColumn(2).setResizable(false);

       jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "类别信息", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 0, 18))); // NOI18N

       jLabel1.setFont(new java.awt.Font("宋体", 0, 18));
       jLabel1.setText("序号：");

       jLabel2.setFont(new java.awt.Font("宋体", 0, 18));
       jLabel2.setText("类别名称：");

       jLabel3.setFont(new java.awt.Font("宋体", 0, 18));
       jLabel3.setText("备注：");

       tid.setFont(new java.awt.Font("宋体", 0, 18));
       tid.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               tidActionPerformed(evt);
           }
       });

       tname.setFont(new java.awt.Font("宋体", 0, 18));

       tmemo.setColumns(20);
       tmemo.setFont(new java.awt.Font("Monospaced", 0, 18));
       tmemo.setRows(5);
       jScrollPane2.setViewportView(tmemo);

       jButton1.setText("保存");
       jButton1.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton1ActionPerformed(evt);
           }
       });

       jButton3.setText("更新");
       jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
           public void mouseClicked(java.awt.event.MouseEvent evt) {
               jButton3MouseClicked(evt);
           }
       });
       jButton3.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton3ActionPerformed(evt);
           }
       });

       jButton4.setText("删除");
       jButton4.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton4ActionPerformed(evt);
           }
       });

       jButton5.setText("新增");
       jButton5.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton5ActionPerformed(evt);
           }
       });

       javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
       jPanel2.setLayout(jPanel2Layout);
       jPanel2Layout.setHorizontalGroup(
           jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(jPanel2Layout.createSequentialGroup()
               .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                   .addGroup(jPanel2Layout.createSequentialGroup()
                       .addGap(39, 39, 39)
                       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addGroup(jPanel2Layout.createSequentialGroup()
                               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                               .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                               .addComponent(tname, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE))
                           .addGroup(jPanel2Layout.createSequentialGroup()
                               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                               .addComponent(jLabel3)
                               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                               .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))
                           .addGroup(jPanel2Layout.createSequentialGroup()
                               .addComponent(jLabel1)
                               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                               .addComponent(tid, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))))
                   .addGroup(jPanel2Layout.createSequentialGroup()
                       .addGap(70, 70, 70)
                       .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                       .addGap(41, 41, 41)
                       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                       .addGap(37, 37, 37)
                       .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                       .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                       .addGap(20, 20, 20)))
               .addGap(83, 83, 83))
       );
       jPanel2Layout.setVerticalGroup(
           jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(jPanel2Layout.createSequentialGroup()
               .addGap(31, 31, 31)
               .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                   .addComponent(tid, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGap(27, 27, 27)
               .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                   .addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                   .addGroup(jPanel2Layout.createSequentialGroup()
                       .addGap(45, 45, 45)
                       .addComponent(jLabel3))
                   .addGroup(jPanel2Layout.createSequentialGroup()
                       .addGap(24, 24, 24)
                       .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
               .addGap(35, 35, 35)
               .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                   .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                   .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                   .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGap(18, 18, 18))
       );

       javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
       jPanel1.setLayout(jPanel1Layout);
       jPanel1Layout.setHorizontalGroup(
           jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(jPanel1Layout.createSequentialGroup()
               .addGap(66, 66, 66)
               .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addContainerGap(55, Short.MAX_VALUE))
           .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE)
       );
       jPanel1Layout.setVerticalGroup(
           jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(jPanel1Layout.createSequentialGroup()
               .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addGap(18, 18, 18)
               .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
               .addContainerGap())
       );

       javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
       getContentPane().setLayout(layout);
       layout.setHorizontalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
       );
       layout.setVerticalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(layout.createSequentialGroup()
               .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
       );

       pack();
   }// </editor-fold>                        

   //表格数据的初始化
   public void initData(){
       //加载数据
       DefaultTableModel dtm = (DefaultTableModel)typeTable.getModel();
       //清空表
       while(dtm.getRowCount()>0){
           dtm.removeRow(0);
       }
       //加载数据
       Set<Integer>set = map.keySet();
       for(Integer i:set){
           TypeBean bean = map.get(i);
           Vector v = new Vector();
           v.add(bean.getId());
           v.add(bean.getTypeName());
           v.add(bean.getMemo());
           dtm.addRow(v);
       }
   }

   private void tidActionPerformed(java.awt.event.ActionEvent evt) {                                    
       // TODO add your handling code here:
}                                   

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       //保存类型操作
      //先保存文本框里的值
       String id = tid.getText();
       String typeName = tname.getText();
       String memo = tmemo.getText();
       //封装成对象
       TypeBean bean = new TypeBean();
       bean.setId(Integer.parseInt(id));
       bean.setMemo(memo);
       bean.setTypeName(typeName);
       //将bean放到map里面
              // Map<Integer,TypeBean>map = new HashMap<Integer,TypeBean>();
       map.put(Integer.parseInt(id), bean);
       //将map放到文件里面
       typeDao.writeType(map);
       //刷新table
       initData();
   }                                        

   private void typeTableComponentAdded(java.awt.event.ContainerEvent evt) {                                         
       // TODO add your handling code here:
}                                        

   private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       // 删除操作
       //获取选中行号及序列号
       int currentRow = typeTable.getSelectedRow();
       int id = (Integer)typeTable.getValueAt(currentRow, 0);
       map.remove(id);
       typeDao.writeType(map);
       JOptionPane.showMessageDialog(this,"类别删除成功");
       initData();
   }                                        

   private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      


   }                                     

   private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       // 更新操作
       //先保存文本框里的值
       int currentRow = typeTable.getSelectedRow();
       int id = (Integer) typeTable.getValueAt(currentRow, 0);
       
       String typeName = tname.getText();
       String memo = tmemo.getText();
       //封装成对象
       TypeBean bean = new TypeBean();
       bean.setId(id);
       bean.setMemo(memo);
       bean.setTypeName(typeName);
       //将bean放到map里面
              // Map<Integer,TypeBean>map = new HashMap<Integer,TypeBean>();
       map.put(id, bean);
       //将map放到文件里面
       typeDao.writeType(map);
       //刷新table
       JOptionPane.showMessageDialog(this,"类别更新成功");
       initData();
   }                                        

   private void typeTableMouseClicked(java.awt.event.MouseEvent evt) {                                       
        //获取选中行号及序列号
       int currentRow = typeTable.getSelectedRow();
        // 将选中的行，显示到信息栏中
       tid.setText((Integer) typeTable.getValueAt(currentRow, 0)+"");
       tname.setText((String) typeTable.getValueAt(currentRow, 1));
       tmemo.setText((String) typeTable.getValueAt(currentRow, 2));

       tid.setEnabled(false);  //序号框不可编辑

   }                                      

   private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       // 新增时，将文本框里的信息进行清空，并将序列号置为可编辑。
       tid.setEnabled(true);
       tid.setText("");
       tname.setText("");
       tmemo.setText("");

   }                                        

   /**
   * @param args the command line arguments
   */
   public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               new TypeForm().setVisible(true);
           }
       });
   }

   // Variables declaration - do not modify                     
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JButton jButton5;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JScrollPane jScrollPane2;
   private javax.swing.JTextField tid;
   private javax.swing.JTextArea tmemo;
   private javax.swing.JTextField tname;
   private javax.swing.JTable typeTable;
   // End of variables declaration                   

}




